package edu.wtbu.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;


import edu.wtbu.dao.UserDao;
import edu.wtbu.pojo.Result;
import edu.wtbu.pojo.User;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter printWriter=response.getWriter();
		
		String Email="";
		String Password="";
		try {
			Email=request.getParameter("Email");
			Password=request.getParameter("Password");
		} catch (Exception e) {
			// TODO: handle exception
		}
		
		User user=UserDao.findbyemandpa(Email,Password);
		if(user!=null)
		{
			Result rr=new Result("success",null,new Object[] {user.getEmail(),user.getRoleId()});
			String mString=JSON.toJSONString(rr);
			printWriter.append(mString);
		}
		else {
			Boolean iBoolean=UserDao.findbyemail(Email);
			if(iBoolean)
			{
				Result rr=new Result("密码错误",null,null);
				String mString=JSON.toJSONString(rr);
				printWriter.append(mString);
			}
			else {

				Result rr=new Result("邮箱不存在",null,null);
				String mString=JSON.toJSONString(rr);
				printWriter.append(mString);
			}
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
